package com.ruoyi.iot.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.iot.domain.PioltDevice;
import com.ruoyi.iot.service.IPioltDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.iot.hikvision.YsDeviceService;
/**
 * 观测站设备Controller
 *
 * @author rww
 * @date 2023-09-18
 */
@RestController
@RequestMapping("/iot/device")
public class PioltDeviceController extends BaseController
{
    @Autowired
    private IPioltDeviceService pioltDeviceService;
    @Autowired
    private YsDeviceService ysDeviceService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询观测站设备列表
     */
//    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(PioltDevice pioltDevice, HttpServletRequest request)
    {
        startPage();
        Long userId = tokenService.getLoginUser(request).getUserId();
        pioltDevice.setUserId(userId);
        List<PioltDevice> list = pioltDeviceService.selectPioltDeviceList(pioltDevice);
        return getDataTable(list);
    }

    /**
     * 导出观测站设备列表
     */
//    @PreAuthorize("@ss.hasPermi('system:device:export')")
    @Log(title = "观测站设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PioltDevice pioltDevice)
    {
        List<PioltDevice> list = pioltDeviceService.selectPioltDeviceList(pioltDevice);
        ExcelUtil<PioltDevice> util = new ExcelUtil<PioltDevice>(PioltDevice.class);
        util.exportExcel(response, list, "观测站设备数据");
    }

    /**
     * 获取观测站设备详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pioltDeviceService.selectPioltDeviceById(id));
    }
    @GetMapping(value = "/byCode/{deviceCode}")
    public AjaxResult getInfoByCode(@PathVariable("deviceCode") String deviceCode)
    {
        PioltDevice device = pioltDeviceService.selectPioltDeviceByDeviceCode(deviceCode);
        return success(device);
    }


    /**
     * 新增观测站设备
     */
//    @PreAuthorize("@ss.hasPermi('system:device:add')")
    @Log(title = "观测站设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PioltDevice pioltDevice, HttpServletRequest request)
    {
        Long userId = tokenService.getLoginUser(request).getUserId();
        pioltDevice.setUserId(userId);
        return toAjax(pioltDeviceService.insertPioltDevice(pioltDevice));
    }

    /**
     * 修改观测站设备
     */
//    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "观测站设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PioltDevice pioltDevice)
    {
        return toAjax(pioltDeviceService.updatePioltDevice(pioltDevice));
    }

    /**
     * 删除观测站设备
     */
//    @PreAuthorize("@ss.hasPermi('system:device:remove')")
    @Log(title = "观测站设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pioltDeviceService.deletePioltDeviceByIds(ids));
    }
    ////////////////////////////////////////
    @PostMapping("/{id}/syncToCloud")
    public AjaxResult syncToCloud(@PathVariable("id") Long id)
    {
        PioltDevice device = pioltDeviceService.selectPioltDeviceById(id);
        if(device==null)return AjaxResult.error("设备不存在");
        if(StringUtils.isEmpty(device.getDeviceSeries())
                ||StringUtils.isEmpty(device.getDeviceNumber())){
            return AjaxResult.error("设备参数错误");
        }
        ysDeviceService.addDeviceSync(device);
        return AjaxResult.success();
    }

    @GetMapping("/{id}/onlinePreview")
    public AjaxResult OnlinePreview(@PathVariable("id") Long id)
    {
        PioltDevice device = pioltDeviceService.selectPioltDeviceById(id);
        if(device==null)return AjaxResult.error("设备不存在");
        if(StringUtils.isEmpty(device.getDeviceSeries())
                ||StringUtils.isEmpty(device.getDeviceNumber())){
            return AjaxResult.error("设备参数错误");
        }
        return AjaxResult.success(ysDeviceService.getDeviceOnlinePreview(device));
    }
}
